package dk.iha;

import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.util.Log;

public class SoundSensor {
	private MediaRecorder recorder = null;
    private MediaPlayer   player = null;
    private static String fileName = null;
    
    public SoundSensor(MediaPlayer player) {
    	this.player = player;
        fileName = SharedData.getFilePath() + File.separator + "audiorecordtest.3gp";
    }
    
    public void startPlaying() {
        try {
            player.setDataSource(fileName);
            player.prepare();
            player.start();
        } catch (IOException e) {
            Log.w("", "prepare() failed");
        }
    }

    public void stopPlaying() {
        player.release();
        player = null;
    }
    
    public void startRecording() {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setOutputFile(fileName);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            recorder.prepare();
        } catch (IOException e) {
            Log.w("", "prepare() failed");
        }

        recorder.start();
    }

    public void stopRecording() {
        recorder.stop();
        recorder.release();
        recorder = null;
    }

}
